angular.module('imsApp').controller('disallowController', ['$scope', '$rootScope', '$state', 'DisallowManager','$filter','$uibModal', function ($scope, $rootScope, $state, DisallowManager,$filter,$uibModal) {
    $scope.search = {pageNo:$state.params.pageNo || 1,
        pageSize:$state.params.pageSize||10,
        type:$state.params.type||"",
        mode:$state.params.mode||'cuid',
        startDay:$state.params.startDay|| null,
        endDay:$state.params.endDay|| null
    };
    $scope.ipType=[{type:null,value:'全部'},{type:401,value: $scope.table[401]},{type:402,value: $scope.table[402]},{type:411,value: $scope.table[411]},{type:412,value: $scope.table[412]},{type:410,value: $scope.table[410]}];
    $scope.cuIdType=[{type:null,value:'全部'},{type:403,value: $scope.table[403]},{type:404,value: $scope.table[404]},{type:413,value: $scope.table[413]},{type:414,value: $scope.table[414]},{type:410,value: $scope.table[410]}];

    $scope.types=$scope.search.mode=='ip'? $scope.ipType:$scope.cuIdType;
    $scope.typeObj=$scope.search.type?$filter("filter")($scope.types,{type:$scope.search.type})[0]:null;
    $scope.statusArray=[{key:500,value:$scope.table[500]},{key:501,value:$scope.table[501]}];
    $scope.typeChange=function (typeObj) {
        if(typeObj){
            $scope.search.type=typeObj.type;

        }else {
            $scope.search.type=null;

        }
        $state.go($state.current.name, $scope.search)

    }

    $scope.rangeDate = {
        startDate:$scope.search.startDay ? moment($scope.search.startDay).format('X'):null,
        endDate:$scope.search.endDay? moment($scope.search.endDay).format('X'):null,
        onSelected: function (data, scope) {  //时间选择确认事件
            $scope.search.startDay=data.startDate?moment(new Date(parseFloat(data.startDate)*1000)).format("YYYY-MM-DD"):null;
            $scope.search.endDay=data.endDate?moment(new Date(new Date(parseFloat(data.endDate)*1000))).format("YYYY-MM-DD"):null;
            $state.go($state.current.name, $scope.search)

        }
    };
    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search)
        }
    };

    $scope.filterClick=function (mode) {
        $scope.search.mode=mode;
        $scope.search.type=null;
        $scope.search.pageNo=1;
        $scope.search.pageSize=10;
        $state.go($state.current.name, $scope.search)

    }


    if($scope.search.mode!="ip"){
        DisallowManager.cuidList([$scope.search.pageNo, $scope.search.pageSize],$scope.search).then(function (result) {
            if(result.data.c=='s'){
                $scope.list=result.data.pageList;
                $scope.paginationConf.totalItems=result.data.total;
            }
        })

    }else {
        DisallowManager.ipList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            if(result.data.c=='s'){
                $scope.list=result.data.pageList;
                $scope.paginationConf.totalItems=result.data.total;
            }
        })
    }

    $scope.allCheckedClick=function (checked) {
        $scope.list.forEach(function (item) {
            item.checked=checked
        })
    }



    
    $scope.add=function () {
        var modalInstance = $uibModal.open({
            animation: true,
            size: 'normal',
            windowClass:'modal-top-20',
            templateUrl: 'disallowAdd.html',
            controller: 'disallowAddModalController',
            resolve: {
                focus:function () {
                    return {};
                },
                types:function () {
                    return $scope.types
                },
                mode:function () {
                    return $scope.search.mode;
                }
            }
        })
        modalInstance.result.then(function (result) {
            if(result=="ok"){
                $state.reload();
            }
        })
    }
    var success=function (result) {
        if(result.data.c=="s"){
            if(result.data.c=='s'){
                $state.reload();
            }
        }
    }

    $scope.unDisallowSelect=function () {
        var ids=[];
        $scope.list.forEach(function (item) {
            if(item.checked){
                ids.push(item.id)
            }
        })
        if($scope.search.mode!="ip"){
            FocusManager.cuIdUnFocus(ids).then(function (result) {
                success(result);
            })
        }else{
            FocusManager.ipUnFocus(ids).then(function (result) {
                success(result);
            })
        }
    }

    $scope.reAllowSelect=function () {
        var ids=[];
        $scope.list.forEach(function (item) {
            if(item.checked){
                ids.push(item.id)
            }
        })
        if($scope.search.mode!="ip"){
            DisallowManager.cuIdReAllow(ids).then(function (result) {
                success(result);
            })
        }else{
            DisallowManager.ipReAllow(ids).then(function (result) {
                success(result);
            })
        }

    }


    
    
    $scope.reAllow=function (id) {
        if($scope.search.mode!="ip"){
            DisallowManager.cuIdReAllow([id]).then(function (result) {
                success(result);
            })
        }else{
            DisallowManager.ipReAllow([id]).then(function (result) {
                success(result);
            })
        }

    }




}])

